package com.xxxx.yebserver.common;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Api(value = "统一结果返回类")
@Data
public class R {

    @ApiModelProperty(value = "响应状态码")
    private Integer code;

    @ApiModelProperty(value = "响应消息")
    private String msg;

    @ApiModelProperty(value = "响应数据")
    private Map<String,Object> data = new HashMap<String,Object>();


    private R(){}

    public static R ok(){
        R r = new R();
        r.setCode(0);
        r.setMsg("成功");
        return r;
    }

    public static R ok(String msg){
        R r = new R();
        r.setCode(0);
        r.setMsg(msg);
        return r;
    }

    public static R error(){
        R r = new R();
        r.setCode(1500);
        r.setMsg("失败");
        return r;
    }

    public static R error(String msg){
        R r = new R();
        r.setCode(1500);
        r.setMsg(msg);
        return r;
    }

    public static R error(Integer code,String msg){
        R r = new R();
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }


    public R msg(String message){
        this.msg = message;
        return this;
    }

    public R code(Integer code){
        this.code = code;
        return this;
    }

    public R data(String k,Object v){
        this.data.put(k,v);
        return this;
    }

    public R data(Object v){
        this.data.put("data",v);
        return this;
    }

    public String getData(){
        return JSON.toJSONString(data.get("data"));
    }
    public String getData(String k){
        return JSON.toJSONString(data.get(k));
    }
}
